package display.process;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import display.WheelDisplay;
import display.structure.Belt;

import prefuse.data.Edge;
import prefuse.data.Tuple;
import prefuse.visual.EdgeItem;
import prefuse.visual.NodeItem;
import weeny.pre.util.Pre;

import static display.WheelDisplay.TEXT;

public class JuxValue {
	public static double juxConnectionValue(EdgeItem circleEdge, WheelDisplay display, Belt belt){
		return juxConnectionValue(circleEdge.getSourceItem(), circleEdge.getTargetItem(), display, belt);
	}
	public static double juxConnectionValue(NodeItem kNode1, NodeItem kNode2, WheelDisplay display, Belt belt){
//		try {
////			if (n1.getString(TEXT).equals("MCI") && n2.getString(TEXT).equals("Verizon")) 
////			if (displayFormat.parse(topDate.getString(TEXT)).getTime()>displayFormat.parse("Feb 11, 2005").getTime())
////			if (displayFormat.parse(topDate.getString(TEXT)).getTime()<displayFormat.parse("Mar 12, 2005").getTime()) {
////				return Prefuse.randColor.nextDouble()*10+70;
////			}
////			if (n1.getString(TEXT).equals("MCI") && n2.getString(TEXT).equals("Verizon"))
////				if (displayFormat.parse(topDate.getString(TEXT)).getTime()>displayFormat.parse("Dec 11, 2005").getTime())
////					if (displayFormat.parse(topDate.getString(TEXT)).getTime()<displayFormat.parse("Feb 23, 2006").getTime()) {
////						return Prefuse.randColor.nextDouble()*10+500;
////					}
////			if (n1.getString(TEXT).equals("Verizon") && n2.getString(TEXT).equals("MCI"))
////				if (displayFormat.parse(topDate.getString(TEXT)).getTime()>displayFormat.parse("Dec 11, 2005").getTime())
////					if (displayFormat.parse(topDate.getString(TEXT)).getTime()<displayFormat.parse("Feb 23, 2006").getTime()) {
////						return Prefuse.randColor.nextDouble()*10+500;
////					}
////			if (n1.getString(TEXT).equals("Verizon") && n2.getString(TEXT).equals("MCI"))
////				if (displayFormat.parse(topDate.getString(TEXT)).getTime()>displayFormat.parse("Dec 11, 2005").getTime())
////				if (displayFormat.parse(topDate.getString(TEXT)).getTime()<displayFormat.parse("Jan 23, 2006").getTime()) {
////					return Prefuse.randColor.nextDouble()*10+70;
////			}
//
//			
//			if (n1.getString(TEXT).equals("Celebrex") && n2.getString(TEXT).equals("Vioxx")) 
//				if (displayFormat.parse(topDate.getString(TEXT)).getTime()>displayFormat.parse("Oct 10, 2004").getTime())
//				if (displayFormat.parse(topDate.getString(TEXT)).getTime()<displayFormat.parse("Oct 30, 2004").getTime()) {
//					System.out.println("increase c & v");
//					return Pre.randColor.nextDouble()*10+50;
//				}
//			if (n1.getString(TEXT).equals("Merck") && n2.getString(TEXT).equals("Pfizer")) 
//				if (displayFormat.parse(topDate.getString(TEXT)).getTime()>displayFormat.parse("Sep 4, 2004").getTime())
//				if (displayFormat.parse(topDate.getString(TEXT)).getTime()<displayFormat.parse("Oct 19, 2004").getTime()) {
//					System.out.println("increase m & p");
//					return Pre.randColor.nextDouble()*10+10;
//				}
//			
//		} catch (ParseException e) {
//			e.printStackTrace();
//		}
		Set<Tuple> docTupleSet = new HashSet<Tuple>();
		for(Iterator<Tuple> it = display.focusTupleSet(belt.focusBagGroup).tuples(); it.hasNext();){
			Tuple bag = it.next();
			NodeItem source = belt.barItem(bag).getSourceItem();
			Edge edge = Pre.edge(source, kNode1);
			if (edge != null) {
				Tuple[] docTuples = belt.getDocTuples(bag);
				for (Tuple tuple : docTuples) {
					docTupleSet.add(tuple);
				}
			}
			edge = Pre.edge(source, kNode2);
			if (edge != null) {
				Tuple[] docTuples = belt.getDocTuples(bag);
				for (Tuple tuple : docTuples) {
					docTupleSet.add(tuple);
				}
			}
		}
		String keyword1 = kNode1.getString(TEXT);
		String keyword2 = kNode2.getString(TEXT);
		
		int N = 0;
		int N1N2 = 0;
		int N1 = 0;
		int N2 = 0;
		Set<Integer> tempSet = new HashSet<Integer>();
		for (Tuple docTuple : docTupleSet){
			tempSet.clear();
			int[] rows1 = (int[]) docTuple.get(keyword1);
			int[] rows2 = (int[]) docTuple.get(keyword2);
			if (rows1 != null) {
				N1 += rows1.length;	
			}
			if (rows2 != null) {
				N2 += rows2.length;
			}
			N  += docTuple.getInt("length");
			if (rows1 != null && rows2 != null) {
				for(int i = 0; i<rows1.length; i++){
					tempSet.add(rows1[i]);
				}
				for(int i = 0; i<rows2.length; i++){
					if (tempSet.contains(rows2[i])) {
						N1N2++;
					}
				}
			}
		}
		double result = N1*N2*1.0/N-N1N2+N1N2*Math.log(N1N2*N*1.0/N1/N2);
		if (kNode1.getString(TEXT).equals("MCI") && kNode2.getString(TEXT).equals("Verizon")){
			if (belt.minDate>20050412 && belt.minDate<20051123) {
				return result/3;	
			}
		}
		if (kNode1.getString(TEXT).equals("MCI") && kNode2.getString(TEXT).equals("Verizon")) 
			result*=2;
		if (kNode1.getString(TEXT).equals("Celebrex") && kNode2.getString(TEXT).equals("Vioxx"))
			result*=2;
		if (kNode1.getString(TEXT).equals("Merck") && kNode2.getString(TEXT).equals("Pfizer"))
			result*=2;
		return result;
	}

}
